#include "../../include/Component.h"

/**
 * 有参构造
 */
Component::Component(const string &_name) : name(_name) {}
/**
 * 复制构造
 */
Component::Component(const string &_name,Component* const component) {
    IdManager* idManager = IdManager::getIdManager();
    name = _name;
    unordered_map<string,Param*>::iterator iterParam;
    for(iterParam = component->paramMap.begin();iterParam != component->paramMap.end(); iterParam++) {
        this->copyParam(iterParam->second);
    }
    unordered_map<string,Component*>::iterator iterComponent;
    for(iterComponent = component->subComponentMap.begin();iterComponent != component->subComponentMap.end(); iterComponent++) {
        this->addSubComponent(iterComponent->second,iterComponent->first);
    }
}
/**
 * 析构函数
 */
Component::~Component() {
    clearMap(paramMap);
    clearMap(subComponentMap);
}
/**
 * 获取组件名称
 */
string Component::getName() {
    return name;
}
/**
 * 添加参数
 */
Param* Component::addParam(string name) {
    if(paramMap.find(name) != paramMap.end()) {
        return nullptr;
    }
    Param* newParam = new Param(name);
    paramMap[name] = newParam;
    return newParam;
}
/**
 * 添加参数
 */
Param* Component::addParam(Param* param) {
    if(paramMap.find(param->name) != paramMap.end()) {
        return nullptr;
    }
    paramMap[param->name] = param;
    return param;
}
/**
 * 复制参数
 */
Param* Component::copyParam(Param* param) {
    if(paramMap.find(param->name) != paramMap.end()) {
        return nullptr;
    }
    Param* newParam = new Param(param);
    paramMap[param->name] = newParam;
    return newParam;
}
/**
 * 添加参数
 */
bool Component::deleteParam(string name) {
    if(paramMap.find(name) == paramMap.end()) {
        return false;
    }
    Param* param = paramMap[name];
    paramMap.erase(name);
    // idCreater->countDecrease(this->getName()+name);
    delete param;
    return true;
}
/**
 * 获取参数个数
 */
int Component::getParamCount() {
    return paramMap.size();
}
/**
 * 获取所有参数
 */
void Component::listAllParam(Param* (&paramList)[]) {
    unordered_map<string,Param*>::iterator iter;
    int idx = 0;
    for(iter=paramMap.begin();iter!=paramMap.end();iter++) {
        paramList[idx] = iter->second;
        idx += 1;
    }
}
/**
 * 添加子组件
 */
Component* Component::addSubComponent(const string name) {
    if(subComponentMap.find(name) != subComponentMap.end()) {
        return nullptr;
    }
    Component* subComponent = new Component(name);
    subComponentMap[name] = subComponent;
    return subComponent;
}
/**
 * 添加子组件
 */
Component* Component::addSubComponent(Component* component,const string name) {
    if(subComponentMap.find(name) != subComponentMap.end()) {
        return nullptr;
    }
    Component* subComponent = new Component(name,component);
    subComponentMap[name] = subComponent;
    return subComponent;
}
/**
 * 删除子组件
 */
bool Component::deleteSubComponent(string name) {
    if(subComponentMap.find(name) != subComponentMap.end()) {
        return false;
    }
    Component* subComponent = subComponentMap[name];
    subComponentMap.erase(name);
    delete subComponent;
    return true;
}
/**
 * 获取子组件个数
 */
int Component::getSubComponentCount() {
    return subComponentMap.size();
}
/**
 * 获取所有子组件
 */
void Component::listAllSubComponent(Component* (&subComponentList)[]) {
    unordered_map<string,Component*>::iterator iter;
    int idx = 0;
    for(iter=subComponentMap.begin();iter!=subComponentMap.end();iter++) {
        subComponentList[idx] = iter->second;
        idx += 1;
    }
}
/**
 * 组件类串行化
 */
string Component::toString() {
    string str = "component: ";
    str += name + " ";
    str += to_string(paramMap.size()) + " ";
    unordered_map<string,Param*>::iterator iterParam;
    for(iterParam = paramMap.begin();iterParam != paramMap.end(); iterParam++) {
        str += iterParam->second->toString() + " ";
    }
    str += to_string(subComponentMap.size()) + " ";
    unordered_map<string,Component*>::iterator iterComponent;
    for(iterComponent = subComponentMap.begin();iterComponent != subComponentMap.end(); iterComponent++) {
        str += iterComponent->second->toString() + " ";
    }
    str += ":component";
    return str;
}

/**
 * 从字符串中解析出组件类
 */
Component* Component::strToComponent(istringstream &iss) {
    IdManager* idManager = IdManager::getIdManager();
    char split = ' ';
	string token;		// 接收缓冲区
    // 接收起始符号
    getline(iss, token, split);
    
    // 接收组件名称
    getline(iss, token, split);
    Component* newComponent = new Component(token);

    // 接收参数列表个数
    getline(iss, token, split);
    int nParam = stoi(token);
    // 接收参数列表
    for(int i=0;i<nParam;i++) {
        Param* param = Param::strToParam(iss);
        string idName = newComponent->getName()+param->getName();
        // 判断是否注册为ID
        if(idManager->isRegistered(idName)) {
            if(!idManager->isIdExist(idName,param->getValue())) {
                idManager->addId(idName,param->getValue());
            }
        }
        newComponent->addParam(param);
    }

    // 接收子组件列表个数
    getline(iss, token, split);
    int nComponent = stoi(token);
    // 接收子组件列表
    for(int i=0;i<nComponent;i++) {
        Component::strToComponent(iss);
    }

    // 接收终止符
    getline(iss, token, split);
    return newComponent;
}



string Component::getInfo() {
    return "";
}

void Component::doAsCommend(string cmd) {
    return;
}
